package com.dingyx.ps.util;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.Charset;

public class HttpUtil {

	private static Logger log = LoggerFactory.getLogger(HttpUtil.class);    //日志记录

	// 访问指定URL获取响应内容
	public static String post(String url,String body)  throws Exception {
		PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
		RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(3000).setSocketTimeout(5000)
				.setExpectContinueEnabled(false).build();
		connManager.setValidateAfterInactivity(1000);
		// 初始化请求客户端实例
		CloseableHttpClient client = HttpClients.custom().setConnectionManager(connManager).build();
		// 新建post请求
		HttpPost post = new HttpPost(url);
		log.info("请求参数"+body);
		post.setEntity(new StringEntity(body));
		// 发送请求
		post.setConfig(requestConfig);
		CloseableHttpResponse response = client.execute(post);
		// 判断是否响应正常
		if (response.getStatusLine().getStatusCode() == 200) {
			return new String(EntityUtils.toByteArray(response.getEntity()));
		}
		if (null != response) {
			response.close();
		}
		if (null != client) {
			client.close();
		}
		return null;
	}

	// 访问指定URL获取响应内容
	public static String get(String url)  throws Exception {
		CloseableHttpClient client = null;
		CloseableHttpResponse response = null;
		PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
		RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(3000).setSocketTimeout(5000)
				.setExpectContinueEnabled(false).build();
		connManager.setValidateAfterInactivity(1000);
		// 初始化请求客户端实例
		client = HttpClients.custom().setConnectionManager(connManager).build();
		// 新建GET请求
		HttpGet post = new HttpGet(url);
		// 发送请求
		post.setConfig(requestConfig);
		response = client.execute(post);
		// 判断是否响应正常
		if (response.getStatusLine().getStatusCode() == 200) {
			return new String(EntityUtils.toByteArray(response.getEntity()));
		}
		if (null != response) {
			response.close();
		}
		if (null != client) {
			client.close();
		}
		return null;
	}

	// 访问指定URL获取响应内容
	public static JSONObject jsonPost(String url, JSONObject body)  throws Exception {
		PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
		RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setSocketTimeout(30000).setConnectionRequestTimeout(30000)
				.setExpectContinueEnabled(false).build();
		connManager.setValidateAfterInactivity(1000);
		// 初始化请求客户端实例
		CloseableHttpClient client = HttpClients.custom().setConnectionManager(connManager).build();
		// 新建post请求
		HttpPost post = new HttpPost(url);
		post.setHeader("Content-Type","application/json;UTF-8");
		StringEntity s = new StringEntity(body.toString(), Charset.forName("UTF-8"));  //对参数进行编码，防止中文乱码
		s.setContentEncoding("UTF-8");
		post.setEntity(s);
		log.info("请求参数"+body);
		// 发送请求
		post.setConfig(requestConfig);
		CloseableHttpResponse response = client.execute(post);
		// 判断是否响应正常
		if (response.getStatusLine().getStatusCode() == 200) {
			// 获取相应内容
			HttpEntity entity = response.getEntity();
			String result = new String(EntityUtils.toByteArray(entity));
			log.info(DateUtil.now() + "|" + url + "|" + result);
			return JSONObject.parseObject(result);
		}
		if (null != response) {
			response.close();
		}
		if (null != client) {
			client.close();
		}
		return null;
	}
	
}
